package cn.zucc.edu.handyzucc.service.impl;

import cn.zucc.edu.handyzucc.dao.BillDao;
import cn.zucc.edu.handyzucc.entity.Bill;
import cn.zucc.edu.handyzucc.entity.BillExtend;
import cn.zucc.edu.handyzucc.service.BillService;
import cn.zucc.edu.handyzucc.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillDao billDao;

    @Override
    public List<Bill> getBillsByDorm(Integer bill_dorm) {
        List<Bill> bills = billDao.getBillsByDorm(bill_dorm);
        bills.get(0).setBill_deadline(bills.get(0).getBill_deadline());
        for (Bill bill : bills
        ) {
            bill.getBill_deadline().setTime(bill.getBill_deadline().getTime() + 1000 * 60 * 60 * 24 * 7);
        }
        return bills;
    }

    @Override
    public List<Bill> getAllBill() {
        return billDao.getAllBill();
    }

    @Override
    public boolean deleteBill(Integer bill_id) {
        return billDao.deleteBill(bill_id) == 1;
    }

    @Override
    public boolean updateBill(Bill bill) {
        return billDao.updateBill(bill) == 1;
    }

    @Override
    public List<BillExtend> batchAddBill(MultipartFile file) {
        List<BillExtend> bills = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getStudentListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 0; i < list.size(); i++) {
                List<Object> billList = list.get(i);

                BillExtend bill = new BillExtend();
                bill.setBill_dorm(Integer.parseInt(billList.get(0).toString()));
                bill.setBill_dorm_building(billList.get(1).toString());
                bill.setBill_dorm_room(billList.get(2).toString());
                bill.setBill_amount(Float.parseFloat(billList.get(3).toString()));
                bill.setBill_period(billList.get(4).toString());

                billDao.addBill(bill);

                bills.add(bill);
            }
        } catch (Exception e){

        }
        return bills;

    }
}
